<?php
namespace Shopmanage\Model;
use Think\Model;
class EventsGiftsModel extends Model {
	public $eid;
	public $wherestr;
	public $rollPage;
	public $listRows;
	public $Page;
	public $field = '`id`,`type`,`status`,`level`,`name`,`details`,`price`,`probability`,`per_user`,`advance_probability`,`isshow`,`quantity`,`pic_url`,`num_iids`,`event_id`,`promoid`,`tagid`,`sid`';
	public $gifts;
	public function getgifts(){
		Vendor('Page');
		$total = $this->where($this->wherestr)->count();
		$this->Page = new \Page($total,$this->listRows);
		$this->Page->rollPage=$this->rollPage;
		$show = $this->Page->show();
		$data = $this->where($this->wherestr)
				  ->limit($this->Page->firstRow.','.$this->Page->listRows)
				  ->getField($this->field);
		foreach($data as $k=>$gift){
			$data[$k]["probability"]=($data[$k]["probability"]/100)."%";
			$data[$k]["level"]=getlevel($data[$k]["level"]);
			foreach($tools as $tool){
				if($data[$k]["type"]==$tool["id"]){
					$data[$k]["type"]=$tool["name"];
				}
			}
		}
		$this->gifts = $data;
	}
	public function getEventGifts(){
		$gifts = $this->where("`sid`=".$this->sid." AND `event_id`=".$this->eid)->getField("`id`,`gift_id`, `type`, `level`, `level_name`, `status`, `name`, `details`, `price`, `probability`,`per_user`,`advance_probability`, `isshow`, `quantity`, `quantity_initial`, `pic_url`, `promoid`, `tagid`, `advance`, `every_minute`, `range_time_start`, `range_time_end`, `every_lotts`, `advance_quantity`, `ext_rule`, `medal_id`, `medal_name`, `medal_pic`,`rule_medal_id`, `rule_medal_name`, `rule_medal_pic`, `medal_quant`, `collect_items`, `cart_items`, `show_title`, `show_content`, `jifen`, `password`, `payment`, `pay_time_start`, `pay_time_end`");
		foreach($gifts as $j=>$gift){
				switch($gift["advance"]){
					case "time-range":
						$gifts[$j]["advance_name"]="指定时段中出";
					break;
					case "ev-minute" :
						$gifts[$j]["advance_name"]="每隔一段时间中出";
					break;
					case "ev-lotts":
						$gifts[$j]["advance_name"]="每多少人中出";
					break;
					default:
						unset($gifts[$j]["advance"]);
					break;
				}
				switch($gift["ext_rule"]){
					case "order":
						$gifts[$j]["ext_rule_name"]="订单兑换";
					break;
					case "jifen":
						$gifts[$j]["ext_rule_name"]="积分兑换";
					break;
					case "password":
						$gifts[$j]["ext_rule_name"]="密码领奖";
					break;
					case "metal":
						$gifts[$j]["ext_rule_name"]="勋章兑换";
					break;
					case "collect":
						if(C("TAE")){
							$collect_items = json_decode(urldecode_utf8($gifts[$j]["collect_items"]));
						}else{
							$collect_items = json_decode(urldecode($gifts[$j]["collect_items"]));
						}
						// $collect_items = explode(",",$gifts[$j]["collect_items"]);
						$gifts[$j]["collect_items_count"]=count($collect_items);
						$gifts[$j]["ext_rule_name"]="收藏单品";
					break;
					case "addcart":
						if(C("TAE")){
							$cart_items = json_decode(urldecode_utf8($gifts[$j]["cart_items"]));
						}else{
							$cart_items = json_decode(urldecode($gifts[$j]["cart_items"]));
						}
						// $cart_items = explode(",",$gifts[$j]["cart_items"]);
						$gifts[$j]["cart_items_count"]=count($cart_items);
						$gifts[$j]["ext_rule_name"]="加购物车";
					break;
					case "friend_show":
						$gifts[$j]["ext_rule_name"]="分享";
					break;
					default:
						unset($gifts[$j]["ext_rule"]);
					break;
				}
				$sub_prob += $gifts[$j]["probability"];
				$gift_names[$gift["gift_id"]]["name"]=$gift["name"];
				$gift_names[$gift["gift_id"]]["pic_url"]=$gift["pic_url"];
				$gift_ids[$gift["gift_id"]]=$gift["gift_id"];
				$gifts[$j]["probability"] = $gifts[$j]["probability"]/100000;
				$gifts[$j]["advance_probability"] = $gifts[$j]["advance_probability"]/100000;
			}
		$this->sub_prob=$sub_prob;
		$this->gifts=$gifts;
		$this->gift_names=$gift_names;
		$this->gift_ids=$gift_ids;
	}
}